<template>
    <el-dialog
      :title="'注册'"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="80px">
        <el-form-item label="公司名" prop="companyName">
          <el-input placeholder="公司名" v-model="dataForm.companyName"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <v-distpicker :province="dataForm.province"
                        :city="dataForm.city"
                        :area="dataForm.area"
                        @province="onChangeProvince"
                        @city="onChangeCity"
                        @area="onChangeArea">
          </v-distpicker>
        </el-form-item>
        <el-form-item label="街道" prop="street">
          <el-input v-model="dataForm.street" placeholder="街道"></el-input>
        </el-form-item>
        <el-form-item label="公司描述" prop="remark">
          <el-input rows="5" type="textarea" v-model="dataForm.remark" placeholder="公司描述"></el-input>
        </el-form-item>
        <el-form-item label="管理帐号" prop="empName">
          <el-input v-model="dataForm.empName" placeholder="管理帐号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="comfirmPassword">
          <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  import VDistpicker from 'v-distpicker'
  export default {
    data () {
      var validateCompanyName = (rule, value, callback) => {
        if (!/\S/.test(value)) {
          callback(new Error('公司名不能为空'))
        } else {
          callback()
        }
      }
      var validateUserName = (rule, value, callback) => {
        if (!/\S/.test(value)) {
          callback(new Error('账号不能为空'))
        } else {
          this.$http({
            url: this.$http.adornUrl('/gomoney-auth/sys/user/checkname'),
            method: 'post',
            data: this.$http.adornData({
              'name': value
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              if (data.emp != null) {
                callback(new Error('账号已经存在'))
              } else {
                callback()
              }
            }
          })
        }
      }
      var validatePassword = (rule, value, callback) => {
        if (!/\S/.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          companyName: '',
          province: '',
          city: '',
          area: '',
          street: '',
          remark: '',
          empName: '',
          password: '',
          comfirmPassword: '',
          email: '',
          mobile: ''
        },
        dataRule: {
          companyName: [
            // { required: true, message: '用户名不能为空', trigger: 'blur' },
            { validator: validateCompanyName, trigger: 'blur' }
          ],
          empName: [
            // { required: true, message: '用户名不能为空', trigger: 'blur' },
            { validator: validateUserName, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      VDistpicker
    },
    methods: {
      init () {
        this.visible = true
      },
      onChangeProvince (data) {
        this.dataForm.province = data.value
      },
      onChangeCity (data) {
        this.dataForm.city = data.value
      },
      onChangeArea (data) {
        this.dataForm.area = data.value
      },
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/gomoney-auth/sys/authentication/register`),
              method: 'post',
              data: this.$http.adornData({
                'companyName': this.dataForm.companyName,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'area': this.dataForm.area,
                'street': this.dataForm.street,
                'remark': this.dataForm.remark,
                'empName': this.dataForm.empName,
                'empPass': this.dataForm.password,
                'empEmail': this.dataForm.email,
                'mobile': this.dataForm.mobile
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
